package com.whut.monitor.service.impl;

import com.whut.monitor.vo.PageFilter;
import com.whut.monitor.vo.Tree;
import com.whut.monitor.dao.IBaseDao;
import com.whut.monitor.model.MachiningEntity;
import com.whut.monitor.service.IMachiningService;
import com.whut.monitor.vo.MachiningVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by CFlower on 2017/11/7.
 */
@Service
public class MachiningServiceImpl implements IMachiningService {
    @Autowired
    private IBaseDao<MachiningEntity> machiningDao;

    @Override
    public void add(MachiningVo machiningVo) {
        MachiningEntity entity = new MachiningEntity();
        entity.setMachiningName(machiningVo.getMachiningName());
        machiningDao.save(entity);
    }
    @Override
    public void delete(Integer id) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        machiningDao.executeHql("delete from MachiningEntity t where t.id=:id",params);
    }
    @Override
    public void edit(MachiningVo machiningVo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("machiningName", machiningVo.getMachiningName());
        params.put("id", machiningVo.getId());
        String hql="update MachiningEntity e set e.machiningName=:machiningName where e.id=:id";
        machiningDao.executeHql(hql, params);
    }
    @Override
    public List<MachiningVo> dataGrid(MachiningVo machiningVo, PageFilter pageFilter){
        List<MachiningVo> ul = new ArrayList<MachiningVo>();
        Map<String, Object> params = new HashMap<String, Object>();
        String hql = " from MachiningEntity t ";
        List<MachiningEntity> list = machiningDao.findList(hql + whereHql(machiningVo, params) + orderHql(pageFilter), params, pageFilter.getPage(), pageFilter.getRows());
        for (MachiningEntity t : list) {
            MachiningVo u = new MachiningVo();
            BeanUtils.copyProperties(t, u);
            ul.add(u);
        }
        return ul;
    }
    @Override
    public Long count(MachiningVo machine, PageFilter ph) {
        Map<String, Object> params = new HashMap<String, Object>();
        String hql = " from MachiningEntity t ";
        return machiningDao.count("select count(*) " + hql + whereHql(machine, params), params);
    }
    private String whereHql(MachiningVo machine, Map<String, Object> params) {
        String hql = "";
        if (machine != null) {
            hql += " where 1=1 ";
            if (machine.getMachiningName() != null) {
                hql += " and t.name like :name";
                params.put("name", "%%" + machine.getMachiningName() + "%%");
            }
        }
        return hql;
    }
    private String orderHql(PageFilter ph) {
        String orderString = "";
        if ((ph.getSort() != null) && (ph.getOrder() != null)) {
            orderString = " order by t." + ph.getSort() + " " + ph.getOrder();
        }
        return orderString;
    }
    @Override
    public List<Tree> tree() {
        List<MachiningEntity> l = null;
        List<Tree> lt = new ArrayList<Tree>();

        l = machiningDao.findList("select distinct entity from MachiningEntity entity");

        if ((l != null) && (l.size() > 0)) {
            for (MachiningEntity r : l) {
                Tree tree = new Tree();
                tree.setId(r.getId().toString());
                tree.setText(r.getMachiningName());
                lt.add(tree);
            }
        }
        return lt;
    }
    @Override
    public List<MachiningEntity> getAllMachining() {
        List<MachiningEntity> list=machiningDao.findList("from MachiningEntity");
        return list;
    }



}
